package org.dromara.bykt.service.impl;

import org.dromara.bykt.domain.BYKTCourse;
import org.dromara.bykt.domain.BYKTCourseCategory;
import org.dromara.bykt.domain.vo.BYKTCourseCategoryVo;
import org.dromara.bykt.domain.vo.BYKTCourseSimulationVo;
import org.dromara.bykt.domain.vo.BYKTCourseVo;
import org.dromara.bykt.mapper.BYKTCourseCategoryMapper;
import org.dromara.bykt.mapper.BYKTCourseMapper;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.dromara.bykt.domain.bo.BYKTSimulationInfoBo;
import org.dromara.bykt.domain.vo.BYKTSimulationInfoVo;
import org.dromara.bykt.domain.BYKTSimulationInfo;
import org.dromara.bykt.mapper.BYKTSimulationInfoMapper;
import org.dromara.bykt.service.IBYKTSimulationInfoService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 试卷信息Service业务层处理
 *
 * @author Hou
 * @date 2025-06-25
 */
@RequiredArgsConstructor
@Service
public class BYKTSimulationInfoServiceImpl implements IBYKTSimulationInfoService {

    private final BYKTSimulationInfoMapper baseMapper;

    private final BYKTCourseMapper courseMapper;

    private final BYKTCourseCategoryMapper courseCategoryMapper;

    /**
     * 查询试卷信息
     *
     * @param id 主键
     * @return 试卷信息
     */
    @Override
    public BYKTSimulationInfoVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询试卷信息列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 试卷信息分页列表
     */
    @Override
    public TableDataInfo<BYKTSimulationInfoVo> queryPageList(BYKTSimulationInfoBo bo, PageQuery pageQuery) {
//        LambdaQueryWrapper<BYKTSimulationInfo> lqw = buildQueryWrapper(bo);
//        Page<BYKTSimulationInfoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        Page<BYKTSimulationInfoVo> result = baseMapper.queryPageList(pageQuery.build(), bo);
        result.getRecords().forEach(this::processRelationData);
        return TableDataInfo.build(result);
    }

    private void processRelationData(BYKTSimulationInfoVo vo) {
        Long courseId = vo.getCourseId();
        BYKTCourseVo byktCourseVo = courseMapper.selectVoOne(new LambdaQueryWrapper<BYKTCourse>().eq(BYKTCourse::getId, courseId));
        if (byktCourseVo != null) {
            vo.setCourseName(byktCourseVo.getTitle());
            vo.setCategoryId(byktCourseVo.getCategoryId());
            BYKTCourseCategoryVo byktCourseCategoryVo = courseCategoryMapper.selectVoOne(new LambdaQueryWrapper<BYKTCourseCategory>().eq(BYKTCourseCategory::getId, byktCourseVo.getCategoryId()));
            if (byktCourseCategoryVo != null) {
                vo.setCategoryName(byktCourseCategoryVo.getName());
            }
        }
    }

    /**
     * 查询符合条件的试卷信息列表
     *
     * @param bo 查询条件
     * @return 试卷信息列表
     */
    @Override
    public List<BYKTSimulationInfoVo> queryList(BYKTSimulationInfoBo bo) {
        LambdaQueryWrapper<BYKTSimulationInfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<BYKTSimulationInfo> buildQueryWrapper(BYKTSimulationInfoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<BYKTSimulationInfo> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(BYKTSimulationInfo::getId);
        lqw.eq(bo.getCourseId() != null, BYKTSimulationInfo::getCourseId, bo.getCourseId());
        lqw.eq(bo.getSimulationId() != null, BYKTSimulationInfo::getSimulationId, bo.getSimulationId());
        lqw.like(StringUtils.isNotBlank(bo.getSimulationName()), BYKTSimulationInfo::getSimulationName, bo.getSimulationName());
        lqw.eq(StringUtils.isNotBlank(bo.getSynopsis()), BYKTSimulationInfo::getSynopsis, bo.getSynopsis());
        lqw.eq(StringUtils.isNotBlank(bo.getImage()), BYKTSimulationInfo::getImage, bo.getImage());
        lqw.eq(bo.getExamTime() != null, BYKTSimulationInfo::getExamTime, bo.getExamTime());
        lqw.orderByAsc(BYKTSimulationInfo::getSort);
        return lqw;
    }

    /**
     * 新增试卷信息
     *
     * @param bo 试卷信息
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(BYKTSimulationInfoBo bo) {
        BYKTSimulationInfo add = MapstructUtils.convert(bo, BYKTSimulationInfo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改试卷信息
     *
     * @param bo 试卷信息
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(BYKTSimulationInfoBo bo) {
        BYKTSimulationInfo update = MapstructUtils.convert(bo, BYKTSimulationInfo.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(BYKTSimulationInfo entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除试卷信息信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }

    @Override
    public Long selectSimulationName(String simulationName) {
        BYKTSimulationInfoVo byktSimulationInfoVo = baseMapper.selectVoOne(new LambdaQueryWrapper<BYKTSimulationInfo>().eq(BYKTSimulationInfo::getSimulationName, simulationName));
        if (byktSimulationInfoVo != null) {
            return byktSimulationInfoVo.getId();
        }
        return 0L;
    }
}
